<?php 
$req_path="../";
include($req_path."includes/ini.php");
include($req_path."includes/db.php");

class M_ClassRoom extends DB{
    private $rowCount=0;
    public $table_name="tb_class_room";
	
    function M_ClassRoom(){
		parent::__construct();
	//$this->dbConn=$this->getDB();
    }
    function setTable($tb_name){
		$this->table_name = "tb_".$tb_name;
	}
	function loadLevel(){
		$sqlscript= "SELECT id, name, alian ,status From tb_class_level ";
	    $result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
    function loadData($id=0,$year=2557){
        
        $sqlscript= " SELECT cr.id, cr.class_id ,cl.name, cr.year , cr.room ,cr.amount , cr.`status`
						,case e1.title when '0' then 'นาย' 
							when '1' then 'นางสาว'
							WHEN '2' then 'นาง'
							else ' ' end  as title1
						 ,e1.name as teacher_1
						 ,case e2.title when '0' then 'นาย' 
							when '1' then 'นางสาว'
							WHEN '2' then 'นาง'
							else ' ' end as title2
						, e2.name as teacher_2
						, ifnull(cr.teacher_1,0) as t1 
						, ifnull(cr.teacher_2,0) as t2 ";
		$sqlscript.= " FROM tb_class_room cr ";
		$sqlscript.= " JOIN tb_class_level cl on cl.id = cr.class_id ";
		$sqlscript.=" LEFT JOIN tb_employee e1 on e1.id = teacher_1 ";
		$sqlscript.=" LEFT JOIN tb_employee e2 on e2.id = teacher_2 ";
		$sqlscript.= " where  cl.`status` = 'Y' and cr.year=".$year ;
		
		if ($id!=0){
			$sqlscript.= " and cr.class_id = ".$id ;
		}
		 
		$sqlscript.=" ORDER BY cr.class_id ,cr.room ";
        
        $result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
        return $result;
    }
    function getRowCount(){
        
        return $this->rowCount;
        
    }	
	function getClassLevel(){
	 
		$sqlscript= "select id, name from tb_class_level where status = 'Y' Order by id asc ";
		
		$result = $this->ExecuteData($sqlscript);
		while($row = $result->FetchRow()){
			$ar[$row["name"]] = $row["id"];
		}
		return $ar;	
	
	}
	
	function getEmployee(){
		$sqlscript= "SELECT case title when '0' then 'นาย' 
					when '1' then 'นางสาว'
					ELSE 'นาง' end as title , name, emp.id  ";
		$sqlscript.= " FROM tb_employee emp 	";
		$result = $this->ExecuteData($sqlscript);
		
		while($row = $result->FetchRow()){
			$ar[$row["id"]] = $row["title"].' ' .$row['name'];
		}
		
		return $ar;	
	}
	function InsertDatalevel($val){
		if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT name,alian,status , createdate, createby FROM  tb_class_level where 1=2 ", $val);
				$this->ExecuteData($sqlInsert);
            }
	}
	function UpdateDatalevel($id,$val){
	  if(isset($val)){
				$sqlUpdate=$this->UpdateSqlScript("SELECT name,alian,status , updatedate, updateby FROM tb_class_level where id='".$id."'" ,$val);
				 $this->ExecuteData($sqlUpdate);
				// return $sqlUpdate;
            }
	}
    function insertData($val){
            if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT year,class_id,room , amount, status, teacher_1, teacher_2,createdate,createby FROM ". $this->table_name." where 1=2 ", $val);
				$this->ExecuteData($sqlInsert);
            }
    }
    function updateData($id,$val){
            if(isset($val)){
				$sqlUpdate=$this->UpdateSqlScript("SELECT year,class_id,room , amount , status, teacher_1, teacher_2, updatedate, updateby FROM ". $this->table_name." where id='".$id."'" ,$val);
				 $this->ExecuteData($sqlUpdate);
				// return $sqlUpdate;
            }
    }
    function deleData($id){
            if(isset($id)){
			$sqlDelete = "Delete FROM ". $this->table_name." where id='".$id."'";
			$this->ExecuteData($sqlDelete);
			return 1;
            }
    }
       function deleDatalevel($id){
            if(isset($id)){
			$sqlDelete = "Delete FROM tb_class_level where id='".$id."'";
			$this->ExecuteData($sqlDelete);
			return 1;
            }
    }
}
?>
